package com.data.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.data.modules.online.auth.service.IOnlAuthDataService;
import com.data.modules.online.cgform.entity.OnlCgformField;
import com.data.modules.online.cgform.entity.OnlCgformHead;
import com.data.modules.online.cgform.model.OnlTable;
import com.data.modules.online.cgform.model.SqlOrder;
import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.util.CgformUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import com.data.common.system.api.ISysBaseAPI;
import com.data.common.system.query.MatchTypeEnum;
import com.data.common.system.util.BootDataAutorUtils;
import com.data.common.system.vo.LoginUser;
import com.data.common.system.vo.SysPermissionDataRuleModel;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.mapper.OnlineMapper;
import com.data.modules.online.cgform.service.IOnlCgformFieldService;
import com.data.modules.online.cgform.service.IOnlCgformHeadService;
import com.data.modules.online.cgform.service.IOnlineJoinQueryService;
import com.data.modules.online.config.datasourceConfig.OnlineFieldConfig;
import com.data.modules.online.config.executor.ConditionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@SuppressWarnings("ALL")
@Slf4j
@Service
public class OnlineJoinQueryServiceImpl implements IOnlineJoinQueryService {

    @Autowired
    @Lazy(true)
    private IOnlAuthDataService onlAuthDataService;
    @Autowired
    IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    @Lazy(true)
    IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private OnlineMapper onlineMapper;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    private static /* synthetic */ java.lang.Object m182a(java.lang.invoke.SerializedLambda r4) {
        /*
        // Method dump skipped, instructions count: 364
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineJoinQueryServiceImpl.m182a(java.lang.invoke.SerializedLambda):java.lang.Object");
    }

    @Override
    public Map<String, Object> pageList(OnlCgformHead head, Map<String, Object> params, boolean ignoreSelectSubField) {
        boolean z;
        List<OnlCgformField> arrayList = new ArrayList<OnlCgformField>();
        String str;
        boolean z2;
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<OnlTable> a = m188a(head, loginUser.getId());
        JSONArray b = CgformUtil.m261b(params);
        MatchTypeEnum c = CgformUtil.m273c(params);
        StringBuilder sb = new StringBuilder();
        boolean z3 = true;
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer stringBuffer2 = new StringBuffer();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        HashMap hashMap = new HashMap();
        HashMap hashMap2 = new HashMap();
        ArrayList arrayList4 = new ArrayList();
        HashMap hashMap3 = new HashMap();
        List<OnlCgformField> arrayList5 = new ArrayList<OnlCgformField>();;
        boolean z4 = false;
        String str2 = "";
        for (OnlTable cVar : a) {
            List<OnlCgformField> selectFieldList = cVar.getSelectFieldList();
            String alias = cVar.getAlias();
            String str3 = alias + ".";
            String str4 = " " + alias + " ";
            String tableName = cVar.getTableName();
            List<OnlCgformField> allFieldList = cVar.getAllFieldList();
            List<SysPermissionDataRuleModel> authList = cVar.getAuthList();
            if (z4 || authList == null || authList.size() <= 0) {
                z = z4;
            } else {
                BootDataAutorUtils.installUserInfo(this.sysBaseAPI.getCacheUser(loginUser.getUsername()));
                z = true;
            }
            ConditionHandler bVar = new ConditionHandler(str3);
            bVar.setNeedList(null);
            bVar.setFirst(false);
            String a2 = bVar.mo1322a(CgformUtil.m287g(allFieldList), params, authList, tableName + "@");
            hashMap3.putAll(bVar.getSqlParams());
            boolean a3 = m194a(params, cVar.mo848a(), tableName, str3, allFieldList, arrayList3);
            boolean a4 = m195a(cVar);
            boolean a5 = m196a(cVar, b);
            boolean z5 = a2.length() > 0;
            if (!(a4 || a5 || z5 || a3)) {
                z4 = z;
            } else {
                boolean z6 = a3 ? false : !a4 && (a5 || z5);
                if ((ignoreSelectSubField && cVar.mo848a()) || (!ignoreSelectSubField && a4)) {
                    m192a(str3, cVar.mo848a(), selectFieldList, arrayList2, hashMap);
                }
                if (!ignoreSelectSubField || !cVar.mo848a()) {
                    arrayList = arrayList5;
                } else {
                    arrayList = selectFieldList;
                }
                String str5 = "";
                ConditionHandler a6 = m191a(cVar, b, c.getValue(), bVar);
                if (a6 != null) {
                    str5 = a6.getSql().toString();
                    if (str5.length() > 0) {
                        hashMap3.putAll(a6.getSqlParams());
                    }
                }
                if (cVar.mo848a()) {
                    stringBuffer.append(CgformUtil.f291b + CgformUtil.m285f(tableName) + str4);
                    str = str3;
                } else {
                    hashMap2.put(alias, tableName);
                    if (z6) {
                        stringBuffer2.append(m186a(cVar, str2, a2, str5));
                        str = str2;
                    } else {
                        stringBuffer.append(" LEFT JOIN ");
                        stringBuffer.append(CgformUtil.m285f(tableName));
                        stringBuffer.append(str4);
                        stringBuffer.append(CgformUtil.f293d);
                        stringBuffer.append(str3);
                        stringBuffer.append(cVar.getJoinField());
                        stringBuffer.append(CgformUtil.f302m);
                        stringBuffer.append(str2);
                        stringBuffer.append(cVar.getMainField());
                        str = str2;
                    }
                }
                if (!z6) {
                    stringBuffer2.append(a2);
                    if (str5.length() > 0) {
                        if (z3) {
                            sb.append(str5);
                            z2 = false;
                            arrayList5 = arrayList;
                            z4 = z;
                            str2 = str;
                            z3 = z2;
                        } else {
                            sb.append(" ").append(c.getValue()).append(" ").append(str5);
                        }
                    }
                }
                z2 = z3;
                arrayList5 = arrayList;
                z4 = z;
                str2 = str;
                z3 = z2;
            }
        }
        String str6 = CgformUtil.select + m185a(arrayList2, hashMap, hashMap2) + stringBuffer.toString() + CgformUtil.f297h + stringBuffer2.toString() + m183a(sb) + m184a(arrayList3);
        HashMap hashMap4 = new HashMap();
        Integer valueOf = Integer.valueOf(params.get("pageSize") == null ? 10 : Integer.parseInt(params.get("pageSize").toString()));
        if (valueOf.intValue() == -521) {
            List<Map<String, Object>> selectByCondition = this.onlineMapper.selectByCondition(str6, hashMap3);
            if (selectByCondition == null || selectByCondition.size() == 0) {
                hashMap4.put("total", 0);
            } else {
                hashMap4.put("total", Integer.valueOf(selectByCondition.size()));
                if (ignoreSelectSubField) {
                    selectByCondition = m197b(selectByCondition);
                }
                hashMap4.put("records", CgformUtil.m238a(selectByCondition, hashMap2.values()));
            }
            if (ignoreSelectSubField) {
                hashMap4.put("fieldList", arrayList5);
            }
        } else {
            IPage<Map<String, Object>> selectPageByCondition = this.onlineMapper.selectPageByCondition(new Page<>((long) Integer.valueOf(params.get("pageNo") == null ? 1 : Integer.parseInt(params.get("pageNo").toString())).intValue(), (long) valueOf.intValue()), str6, hashMap3);
            hashMap4.put("total", Long.valueOf(selectPageByCondition.getTotal()));
            List<Map<String, Object>> records = selectPageByCondition.getRecords();
            if (ignoreSelectSubField) {
                records = m197b(records);
            }
            hashMap4.put("records", CgformUtil.m238a(records, hashMap2.values()));
        }
        return hashMap4;
    }

    private String m186a(OnlTable cVar, String str, String str2, String str3) {
        String alias = cVar.getAlias();
        String f = CgformUtil.m285f(cVar.getTableName());
        String str4 = alias + ".";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" AND EXISTS (");
        stringBuffer.append(CgformUtil.select);
        stringBuffer.append(str4 + "id");
        stringBuffer.append(CgformUtil.f291b);
        stringBuffer.append(f);
        stringBuffer.append(" " + alias);
        stringBuffer.append(CgformUtil.f298i);
        stringBuffer.append(str4);
        stringBuffer.append(cVar.getJoinField());
        stringBuffer.append(CgformUtil.f302m);
        stringBuffer.append(str);
        stringBuffer.append(cVar.getMainField());
        if (str2 != null && str2.length() > 0) {
            stringBuffer.append(str2);
        }
        if (str3 != null && str3.length() > 0) {
            stringBuffer.append(" AND (").append(str3).append(") ");
        }
        stringBuffer.append(")");
        return stringBuffer.toString();
    }

    @Override
    public Map<String, Object> pageList(OnlCgformHead head, Map<String, Object> params) {
        return pageList(head, params, false);
    }

    private String m185a(List<String> list, Map<String, Integer> map, Map<String, String> map2) {
        ArrayList arrayList = new ArrayList();
        for (String str : list) {
            String[] split = str.split("\\.");
            String str2 = split[0];
            if ("a".equals(str2)) {
                arrayList.add(str);
            } else {
                String str3 = split[1];
                if (map.get(str3).intValue() > 1) {
                    arrayList.add(str + " " + map2.get(str2) + "_" + str3);
                } else {
                    arrayList.add(str);
                }
            }
        }
        return String.join(CgformUtil.f231E, arrayList);
    }

    private void m192a(String str, boolean z, List<OnlCgformField> list, List<String> list2, Map<String, Integer> map) {
        if (list != null && list.size() != 0) {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                OnlCgformField onlCgformField = list.get(i);
                String dbFieldName = onlCgformField.getDbFieldName();
                if (!"id".equals(dbFieldName) && 1 == onlCgformField.getIsShowList().intValue()) {
                    if (CgformUtil.f244R.equals(onlCgformField.getFieldShowType()) && ConvertUtils.isNotEmpty(onlCgformField.getDictText())) {
                        list2.add(str + onlCgformField.getDictText());
                    }
                    list2.add(str + dbFieldName);
                    Integer num = map.get(dbFieldName);
                    if (num == null) {
                        map.put(dbFieldName, 1);
                    } else {
                        map.put(dbFieldName, Integer.valueOf(num.intValue() + 1));
                    }
                }
            }
            list2.add(str + "id");
            map.put("id", 2);
        } else if (z) {
            list2.add(str + "id");
        }
    }

    private OnlTable m190a(OnlCgformHead onlCgformHead, int i, boolean z) {
        String id = onlCgformHead.getId();
        String tableName = onlCgformHead.getTableName();
        OnlTable cVar = new OnlTable(tableName, id, z);
        List<OnlCgformField> a = m187a(id);
        List<OnlCgformField> queryAvailableFields = this.onlCgformFieldService.queryAvailableFields(id, tableName, true, a, null);
        cVar.setAllFieldList(a);
        cVar.setSelectFieldList(queryAvailableFields);
        cVar.setAliasByIntValue(i);
        if (!z) {
            Iterator<OnlCgformField> it = a.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                OnlCgformField next = it.next();
                if (ConvertUtils.isNotEmpty(next.getMainField()) && ConvertUtils.isNotEmpty(next.getMainTable())) {
                    cVar.setMainField(next.getMainField());
                    cVar.setJoinField(next.getDbFieldName());
                    break;
                }
            }
        }
        return cVar;
    }

private java.util.List<OnlTable> m188a(OnlCgformHead r11, java.lang.String r12) {
        /*
        // Method dump skipped, instructions count: 126
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineJoinQueryServiceImpl.m188a(org.jeecg.modules.online.cgform.entity.OnlCgformHead, java.lang.String):java.util.List");
    }

private java.util.Map<java.lang.String, java.util.List<OnlCgformField>> m189a(OnlCgformHead r9, java.util.Map<java.lang.String, java.lang.String> r10) {
        /*
        // Method dump skipped, instructions count: 128
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineJoinQueryServiceImpl.m189a(org.jeecg.modules.online.cgform.entity.OnlCgformHead, java.util.Map):java.util.Map");
    }

private java.util.List<OnlCgformField> m187a(java.lang.String r3) {
        /*
            r2 = this;
            com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper r0 = new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
            void r1 = r0.<init>()
            java.lang.Object r1 = r0.eq(r1, r3)
            r0.orderByAsc(r1)
            org.jeecg.modules.online.cgform.service.IOnlCgformFieldService r1 = r2.onlCgformFieldService
            java.util.List r0 = r1.list(r0)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineJoinQueryServiceImpl.m187a(java.lang.String):java.util.List");
    }

    private boolean m194a(Map<String, Object> map, boolean z, String str, String str2, List<OnlCgformField> list, List<SqlOrder> list2) {
        String string;
        boolean z2 = z;
        Object obj = map.get("column");
        if (obj == null || "id".equals(obj.toString())) {
            for (OnlCgformField onlCgformField : list) {
                if ("1".equals(onlCgformField.getSortFlag())) {
                    String fieldExtendJson = onlCgformField.getFieldExtendJson();
                    SqlOrder eVar = new SqlOrder(onlCgformField.getDbFieldName());
                    eVar.setAlias(str2);
                    if (fieldExtendJson != null && !"".equals(fieldExtendJson) && (string = JSON.parseObject(fieldExtendJson).getString(ExtendJsonKey.f93g)) != null && !"".equals(string)) {
                        eVar.setRule(string);
                    }
                    list2.add(eVar);
                    z2 = true;
                } else {
                    z2 = z2;
                }
            }
            return z2;
        }
        String obj2 = obj.toString();
        Object obj3 = map.get("order");
        String str3 = "desc";
        if (obj3 != null) {
            str3 = obj3.toString();
        }
        if (z) {
            if (!CgformUtil.m277c(obj2, list)) {
                return z2;
            }
            SqlOrder eVar2 = new SqlOrder(obj2, str3);
            eVar2.setAlias(str2);
            list2.add(eVar2);
            return z2;
        } else if (!obj2.startsWith(str)) {
            return z2;
        } else {
            String replaceFirst = obj2.replaceFirst(str + "_", "");
            if (!CgformUtil.m277c(replaceFirst, list)) {
                return z2;
            }
            SqlOrder eVar3 = new SqlOrder(replaceFirst, str3);
            eVar3.setAlias(str2);
            list2.add(eVar3);
            return true;
        }
    }

    private String m184a(List<SqlOrder> list) {
        if (list.size() == 0) {
            list.add(SqlOrder.m145a("a."));
        }
        ArrayList arrayList = new ArrayList();
        for (SqlOrder eVar : list) {
            arrayList.add(eVar.getRealSql());
        }
        return " ORDER BY " + String.join(CgformUtil.f231E, arrayList);
    }

    private String m183a(StringBuilder sb) {
        String sb2 = sb.toString();
        if (sb2 == null || "".equals(sb2)) {
            return "";
        }
        return " AND (" + sb2 + ") ";
    }

    private boolean m193a(StringBuilder sb, JSONArray jSONArray, MatchTypeEnum matchTypeEnum, String str, String str2, List<OnlCgformField> list, boolean z, boolean z2) {
        if (jSONArray == null) {
            return z2;
        }
        boolean z3 = z2;
        for (int i = 0; i < jSONArray.size(); i++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i);
            String string = jSONObject.getString("field");
            String[] split = string.split(CgformUtil.f231E);
            if (split.length != 1) {
                String str3 = split[1];
                if (str2.equalsIgnoreCase(split[0]) && CgformUtil.m277c(str3, list)) {
                    CgformUtil.m247a(sb, str + str3, jSONObject, matchTypeEnum, null, z3);
                    z3 = false;
                }
            } else if (z && CgformUtil.m277c(string, list)) {
                CgformUtil.m247a(sb, str + string, jSONObject, matchTypeEnum, null, z3);
                z3 = false;
            }
        }
        return z3;
    }

    private List<Map<String, Object>> m197b(List<Map<String, Object>> list) {
        HashMap hashMap = new HashMap();
        for (Map<String, Object> map : list) {
            String obj = map.get("id").toString();
            if (hashMap.get(obj) == null) {
                hashMap.put(obj, map);
            }
        }
        return new ArrayList(hashMap.values());
    }

    private boolean m196a(OnlTable cVar, JSONArray jSONArray) {
        if (cVar.mo848a()) {
            return true;
        }
        String tableName = cVar.getTableName();
        if (jSONArray == null || jSONArray.size() <= 0) {
            return false;
        }
        for (int i = 0; i < jSONArray.size(); i++) {
            String[] split = jSONArray.getJSONObject(i).getString("field").split(CgformUtil.f231E);
            if (split.length == 2 && split[0] != null && split[0].equals(tableName)) {
                return true;
            }
        }
        return false;
    }

private boolean m195a(OnlTable r5) {
        /*
            r4 = this;
            r1 = 1
            boolean r0 = r5.mo848a()
            if (r0 == 0) goto L_0x0009
            r0 = r1
        L_0x0008:
            return r0
        L_0x0009:
            java.util.List r0 = r5.getSelectFieldList()
            if (r0 == 0) goto L_0x0035
            int r2 = r0.size()
            if (r2 <= 0) goto L_0x0035
            java.util.Iterator r2 = r0.iterator()
        L_0x0019:
            boolean r0 = r2.hasNext()
            if (r0 == 0) goto L_0x0035
            java.lang.Object r0 = r2.next()
            org.jeecg.modules.online.cgform.entity.OnlCgformField r0 = (org.jeecg.modules.online.cgform.entity.OnlCgformField) r0
            java.lang.String r0 = r0.getMainTable()
            if (r0 == 0) goto L_0x0033
            java.lang.String r3 = ""
            boolean r0 = r3.equals(r0)
            if (r0 == 0) goto L_0x0019
        L_0x0033:
            r0 = r1
            goto L_0x0008
        L_0x0035:
            r0 = 0
            goto L_0x0008
        */
        throw new UnsupportedOperationException("Method not decompiled: com.data.modules.online.cgform.service.p013a.OnlineJoinQueryServiceImpl.m195a(org.jeecg.modules.online.cgform.model.c):boolean");
    }

    private ConditionHandler m191a(OnlTable cVar, JSONArray jSONArray, String str, ConditionHandler bVar) {
        String tableName = cVar.getTableName();
        boolean a = cVar.mo848a();
        List<OnlCgformField> allFieldList = cVar.getAllFieldList();
        ArrayList arrayList = new ArrayList();
        if (jSONArray != null) {
            for (int i = 0; i < jSONArray.size(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                String string = jSONObject.getString("field");
                String[] split = string.split(CgformUtil.f231E);
                if (split.length != 1) {
                    String str2 = split[1];
                    if (tableName.equalsIgnoreCase(split[0]) && CgformUtil.m277c(str2, allFieldList)) {
                        arrayList.add(new OnlineFieldConfig(jSONObject));
                    }
                } else if (a && CgformUtil.m277c(string, allFieldList)) {
                    arrayList.add(new OnlineFieldConfig(jSONObject));
                }
            }
            if (arrayList.size() > 0) {
                ConditionHandler bVar2 = new ConditionHandler(cVar.getAlias() + ".", true, str);
                bVar2.setDuplicateSqlNameRecord(bVar.getDuplicateSqlNameRecord());
                bVar2.setDuplicateParamNameRecord(bVar.getDuplicateParamNameRecord());
                bVar2.mo1319a((List<OnlineFieldConfig>) arrayList);
                return bVar2;
            }
        }
        return null;
    }
}
